package untiring.controllers;



import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

import untiring.helper.SimpleCaptchaUtil;
import untiring.interfaces.MemberService;
import untiring.models.MemberVO;

@Controller
@SessionAttributes({"loginUID","loginName"}) //세션변수
public class IndexController {
	
	@Autowired
	private SimpleCaptchaUtil captchaUtil;
	@Autowired
	private MemberService memberserviece;
	
	@RequestMapping(value="/index", method = RequestMethod.GET)
	public String index() throws Exception{
		return "index";
	}
	
	@RequestMapping(value="/intro/intro", method = RequestMethod.GET)
	public String intro() throws Exception{
		return "intro/intro";
	}
	

	@RequestMapping(value="/event/list", method = RequestMethod.GET)
	public String event() throws Exception{
		return "event/list";
	}
	
	@RequestMapping(value="/event/view", method = RequestMethod.GET)
	public String eview() throws Exception{
		return "event/view";
	}
	
	@RequestMapping(value="/mypage/mypage", method = RequestMethod.GET)
	public String mypage() throws Exception{
		return "mypage/mypage";

	}
	
	@RequestMapping(value="/mypage/myinfo", method = RequestMethod.GET)
	public String myinfo() throws Exception{
		return "mypage/myinfo";

	}
	
	
	@RequestMapping(value="/admin/admin", method = RequestMethod.GET)
	public String admin() throws Exception{
		return "admin/admin";

	}
	@RequestMapping(value="/admin/failed", method = RequestMethod.GET)
	public String failed() throws Exception{
		return "admin/failed";

	}
	
	@RequestMapping(value="/admin/login", method = RequestMethod.POST)
	public String login(@ModelAttribute MemberVO m, ModelMap model) throws Exception{
		String result = "redirect:/admin/failed.mvc";
		MemberVO mv = memberserviece.login(m);
		if(memberserviece.login(m) != null){
			model.put("loginUID", mv.getUid()); //세션 생성
			model.put("loginName", mv.getName()); //세션 생성
			System.out.println(mv.getName());
			System.out.println(mv.getUid());
			// 세션 생성시 sessionAttibute 변수명과 일치
			// 즉, sessionAttribute 변수명은 loginUID임
			result = "redirect:/index.mvc";
		}
		return result;
	}
	
	@RequestMapping(value="/admin/logout", method = RequestMethod.GET)
	public String logout(SessionStatus sess) throws Exception{
		sess.setComplete(); //세션 제거
		return "redirect:/index.mvc";
	}
	
	@RequestMapping(value="/admin/captcha", method = RequestMethod.GET)
	public void captcha(HttpServletResponse res , HttpSession sess) throws Exception{
		captchaUtil.captcha(res , sess);
	}
	
	@RequestMapping(value="/admin/checkcaptcha", method = RequestMethod.GET)
	@ResponseBody
	public String captcha(HttpSession sess, @RequestParam String mykey) throws Exception{
		if(sess.getAttribute("captcha").equals(mykey))
			return "y";
		else
			return "n";
	}
	
	
}

